﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Vertica.Data.VerticaClient;
using System.Windows.Threading;
using Hp.Vertica.Client.Internal;

namespace Hp.Vertica.Client
{
    /// <summary>
    /// Interaction logic for Connection.xaml
    /// </summary>
    public partial class VerticaConnection : Window
    {
        public VerticaDatabase DatabaseContext { get; private set; }
        private bool _isCanceled = false;

        private void enableUI()
        {
            progressBar.Visibility = System.Windows.Visibility.Collapsed;
            panelForm.IsEnabled = true;
            btnSave.Visibility = System.Windows.Visibility.Visible;
        }

        private void disableUI()
        {
            progressBar.Visibility = System.Windows.Visibility.Visible;
            panelForm.IsEnabled = false;
            btnSave.Visibility = System.Windows.Visibility.Collapsed;
        }

        public VerticaConnection()
        {
            InitializeComponent();

            txtHost.Focus();
            btnCancel.Click += (s, e) =>
            {
                _isCanceled = true;
                enableUI();
            };
            btnSave.Click += (s, e) =>
            {
                _isCanceled = false;
                if (String.IsNullOrWhiteSpace(txtHost.Text))
                {
                    txtHost.Focus();
                }
                else if (String.IsNullOrWhiteSpace(txtUser.Text))
                {
                    txtUser.Focus();
                }
                else if (String.IsNullOrWhiteSpace(txtPassword.Password))
                {
                    txtPassword.Focus();
                }
                else if (String.IsNullOrWhiteSpace(txtDatabase.Text))
                {
                    txtDatabase.Focus();
                }
                else
                {
                    this.disableUI();
                    VerticaDatabase context = new VerticaDatabase();
                    context.Host = txtHost.Text;
                    context.User = txtUser.Text;
                    context.Password = txtPassword.Password;
                    context.Database = txtDatabase.Text;

                    Action<VerticaDatabase> connect = (c) =>
                    {
                        try
                        {
                            c.Connect();
                            panelMain.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
                            {
                                if (!_isCanceled)
                                {
                                    this.DatabaseContext = c;
                                    this.Close();
                                }
                            }));
                        }
                        catch (Exception exc)
                        {
                            panelMain.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
                            {
                                if (!_isCanceled)
                                {
                                    this.DatabaseContext = null;
                                    this.enableUI();
                                    MessageBox.Show(exc.Message);
                                }
                            }));
                        }
                    };
                    connect.BeginInvoke(context, null, null);
                }
            };
        }

        public VerticaConnection(VerticaDatabase conn)
            : this()
        {
            if (conn != null)
            {
                txtHost.Text = conn.Host;
                txtUser.Text = conn.User;
                txtPassword.Password = conn.Password;
                txtDatabase.Text = conn.Database;
            }
        }

    }
}
